<?php
	
	// Add RSS links to <head> section
	automatic_feed_links();
	
	// Load jQuery
	if ( !is_admin() ) {
	   wp_deregister_script('jquery');
	   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"), false);
	   wp_enqueue_script('jquery');
	}
	
	// Clean up the <head>
	function removeHeadLinks() {
    	remove_action('wp_head', 'rsd_link');
    	remove_action('wp_head', 'wlwmanifest_link');
    }
    add_action('init', 'removeHeadLinks');
    remove_action('wp_head', 'wp_generator');
    
    if (function_exists('register_sidebar')) {
    	register_sidebar(array(
    		'name' => 'Sidebar Widgets',
    		'id'   => 'sidebar-widgets',
    		'description'   => 'These are widgets for the sidebar.',
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h2>',
    		'after_title'   => '</h2>'
    	));
    }
	
	
	add_action('init','create_post_type');
	function create_post_type(){
		register_post_type('c4der_portfolio',
			array(
				'labels' => array(
					'name' 			=> __('Portfolio'),
					'sigular_name' 	=> __('Portfolio')
				),
				'public' 		=> true,
				'has_archive'	=> true,
				'rewrite' 		=> array('slug' => 'portfolio','with_front' => FALSE),
				'supports' 		=> array(
					'title',
					'editor',
					'thumbnail',
					'excerpt'
				)
			)
		);
		
		// Very important in custom posttypes
		flush_rewrite_rules();
	}

	
	// Enable Thumbnail.
	add_theme_support( 'post-thumbnails' );
	
	function get_first_image() {
		global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$first_img = $matches [1] [0];

		if(empty($first_img) || !check_url_exist($first_img)){ //Defines a default image
			$first_img = "http://tink30b.com/dev/wp-content/themes/portfolio/images/default.jpg";
		}
		return $first_img;
	}
	
	function check_url_exist($url){
		$url = @parse_url($url); 
		if (!$url) return false; 
		 
		$url = array_map('trim', $url); 
		$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port']; 
		 
		$path = (isset($url['path'])) ? $url['path'] : '/'; 
		$path .= (isset($url['query'])) ? "?$url[query]" : ''; 
		 
		if (isset($url['host']) && $url['host'] != gethostbyname($url['host'])) { 
		 
			 $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30); 
		 
			  if (!$fp) return false; //socket not opened
		 
				fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n"); //socket opened
				$headers = fread($fp, 4096); 
				fclose($fp); 
		 
			 if(preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers)){//matching header
				   return true; 
			 } 
			 else return false;
		 
		 } // if parse url
		 else return false;
	}
	
	function c4der_comment($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
			<div id="comment-<?php comment_ID(); ?>">
			
			<?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>

			<?php printf(__('<h3 class="fn">%s</h3>'), get_comment_author_link()) ?>
			
			<?php if ($comment->comment_approved == '0') : ?>
				<em><?php _e('Your comment is awaiting moderation.') ?></em>
				<br />
			<?php endif; ?>
			
			<p class="datetime">
				<span class="icon_timeDate">&nbsp;</span>
				<?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>
			</p>
			<div class="fixed"></div>
			
			<div class="commentContent">
				<?php comment_text() ?>
				<div class="fixed"></div>
			</div>		
			<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
			
		 </div>
		<?php
        }
	//Register sidebar

	register_sidebar(
		array(
			'name' => 'Left Sidebar',
			'before_widget' => '<div class="siderbar-item">',
			'after_widget' => '</div>',
			'before_title' => '<h4>',
			'after_title' => '</h4>'
		)
	);
	
?>